package com.ushio.gougouwangguo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ushio.commonmodule.pojo.result.Result;
import com.ushio.gougouwangguo.entity.mysql.Materials;
import com.ushio.gougouwangguo.entity.qo.MaterialsUpDateQo;
import com.ushio.gougouwangguo.entity.vo.MaterialsVo;
import com.ushio.gougouwangguo.enums.MaterialsTypeEnum;
import com.ushio.gougouwangguo.service.MaterialsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/materials")
@Tag(name="物资",description = "物资,项目的基础组成部分,物资名称,物资描述,等等")
public class MaterialsController {

    @Autowired
    MaterialsService materialsService;


    @GetMapping("/all")
    @Operation(summary = "查询所有物资信息")
    private Result<List<MaterialsVo>> getMaterialsAll(){
        try{

            List<MaterialsVo> materialsVos =   materialsService.getMaterialsAll();

            return Result.success(materialsVos);

        }catch (Exception e){

            e.printStackTrace();

            return Result.fail(e.getMessage());
        }
    }
    @GetMapping("/page")
    @Operation(summary = "查询所有物资信息")
    private Result<Page<MaterialsVo>> getMaterialsPage(
            @RequestParam(value = "materialsName",required = false) String materialsName,
            @RequestParam(value = "materialsQualityId",required = false) Byte materialsQualityId,
            @RequestParam(value = "materialsTypeId",required = false) String materialsTypeId,
            @RequestParam(value = "materialsDescribe",required = false) String materialsDescribe,
            @RequestParam(value = "materialsRole",required = false) String materialsRole,
            @RequestParam(value = "page",required = false) Integer page,
            @RequestParam(value = "size",required = false) Integer size)
            {
        try{

            int current = (page == null || page <= 0) ? 1 : page;
            int pageSize = (size == null || size <= 0) ? 10 : size;

            Page<Materials> pageParam = new Page<>(current, pageSize);

            Page<MaterialsVo> materialsVos =   materialsService.getMaterialsPage(pageParam,materialsName,materialsQualityId,materialsTypeId,materialsDescribe,materialsRole);

            return Result.success(materialsVos);

        }catch (Exception e){

            e.printStackTrace();

            return Result.fail(e.getMessage());
        }
    }


    @GetMapping("/type-enum-all")
    @Operation(summary = "查询所有物资信息")
    private Result<List<Map<String, Object>>> getMaterialsTypeEnumAll(){
        try{

            return Result.success(MaterialsTypeEnum.toList());

        }catch (Exception e){

            e.printStackTrace();

            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/update")
    @Operation(summary = "查询所有物资信息")
    private Result updateMaterials(@RequestBody MaterialsUpDateQo materialsUpDateQo){
        try{
            materialsService.updateMaterials(materialsUpDateQo);
            return Result.success();

        }catch (Exception e){

            e.printStackTrace();

            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/delete")
    @Operation(summary = "查询所有物资信息")
    private Result deleteMaterials(@RequestBody MaterialsUpDateQo materialsUpDateQo){
        try{
            materialsService.deleteMaterials(materialsUpDateQo);
            return Result.success();

        }catch (Exception e){

            e.printStackTrace();

            return Result.fail(e.getMessage());
        }
    }

}
